﻿using FlaUI.Core.AutomationElements;
using System;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace QQTools.Utils
{
    public static class Utils
    {

        private static readonly object _clipboardLock = new object();

        public static void ClearAndSendKeys(AutomationElement element, string keys)
        {
            if (element != null)
            {
                element.Click();
                SendKeys.SendWait("{DEL}");
                SendKeys.SendWait(keys);
            }
        }

        // 线程安全的随机数生成器
        private static readonly Random _random = new Random();
        private static readonly object _lock = new object();
        /// <summary>
        /// 线程安全的随机数生成器
        /// </summary>
        public static class ThreadSafeRandom
        {
            public static int Next(int minValue, int maxValue)
            {
                lock (_lock)
                {
                    return _random.Next(minValue, maxValue);
                }
            }
        }

        /// <summary>
        /// 将本地图片文件复制到剪贴板
        /// </summary>
        /// <param name="imagePath">图片文件路径</param>
        public static void CopyImageToClipboard(string imagePath)
        {
            // 1. 检查文件是否存在
            if (!File.Exists(imagePath))
            {
                throw new FileNotFoundException($"图片文件不存在：{imagePath}");
            }

            // 2. 加载图片
            using (var image = Image.FromFile(imagePath))
            {
                // 3. 将图片写入剪贴板
                RunInSTAThread(() =>
                {
                    lock (_clipboardLock)
                    {
                        Clipboard.SetImage(image);
                    }
                });
            }
        }

        /// <summary>
        /// 从剪贴板读取内容
        /// </summary>
        /// <returns>剪贴板中的内容</returns>
        public static object GetClipboardContent()
        {
            return RunInSTAThread<object>(() =>
            {
                if (Clipboard.ContainsImage())
                {
                    return Clipboard.GetImage();
                }
                else if (Clipboard.ContainsText())
                {
                    return Clipboard.GetText();
                }
                else if (Clipboard.ContainsFileDropList())
                {
                    return Clipboard.GetFileDropList();
                }
                else
                {
                    return null;
                }
            });
        }

        /// <summary>
        /// 还原剪贴板内容
        /// </summary>
        /// <param name="content">之前的剪贴板内容</param>
        public static void RestoreClipboardContent(object content)
        {
            RunInSTAThread(() =>
            {
                lock (_clipboardLock)
                {
                    if (content is Image image)
                    {
                        Clipboard.SetImage(image);
                    }
                    else if (content is string text)
                    {
                        Clipboard.SetText(text);
                    }
                    else if (content is System.Collections.Specialized.StringCollection fileDropList)
                    {
                        Clipboard.SetFileDropList(fileDropList);
                    }
                    else if (content == null)
                    {
                        Clipboard.Clear();
                    }
                }
            });
        }
        /// <summary>
        /// 在单线程单元 (STA) 线程中运行指定的函数并返回结果
        /// </summary>
        /// <typeparam name="T">函数的返回类型</typeparam>
        /// <param name="func">要运行的函数</param>
        /// <returns>函数的返回结果</returns>
        private static T RunInSTAThread<T>(Func<T> func)
        {
            T result = default;
            Thread thread = new Thread(() =>
            {
                result = func();
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            return result;
        }

        /// <summary>
        /// 在单线程单元 (STA) 线程中运行指定的操作
        /// </summary>
        /// <param name="action">要运行的操作</param>
        private static void RunInSTAThread(Action action)
        {
            Thread thread = new Thread(() =>
            {
                action();
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
        }
    }
}
